﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Web;

namespace SAIM.Models
{
    public class Security
    {
        public static string Hash(string fileName)
        {
            //create salt
            byte[] salt;
            new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]);

            //create hash
            var pbkdf2 = new Rfc2898DeriveBytes(DateTime.UtcNow.ToLongDateString() + fileName, salt);
            var hash = pbkdf2.GetBytes(16);

            //combine salt and hash
            var hashBytes = new byte[32];
            Array.Copy(salt, 0, hashBytes, 0, 16);
            Array.Copy(hash, 0, hashBytes, 16, 16);

            //convert to base64
            var base64Hash = Convert.ToBase64String(hashBytes);

            return Regex.Replace(base64Hash, "[^A-Za-z0-9]", String.Empty);
        }
    }
}